package com.game.grid;

import com.mcodesal.framework.Sprite2D;

import android.graphics.Bitmap;
import android.graphics.Rect;

public class AnimatedSprite extends Sprite2D {
    private float mSrcX;
    private float mSrcY;
    private float mSrcH;
    private float mSrcW;
    private int mMsPF;
    private int mNumFrames;
    private int mCurrentFrame;
    private long mFrameTimer;
    public AnimatedSprite(int w, int h, int srcW, int srcH, int frameW, int frameH, int numFrames, int MsPF) {
		super(w, h);
		mMsPF = MsPF;
		mNumFrames = numFrames;
		mSrcX = mSrcY = 0f;
		mSrcH = (float)frameH/(float)srcH;
		mSrcW = (float)frameW/(float)srcW;
		mFrameTimer = -1;
		mCurrentFrame = 0;
	}
    
    public void update(long gameTime) {
    	if(mFrameTimer == -1)
    		mFrameTimer = gameTime;
    	if(gameTime > mFrameTimer + mMsPF ) {
            mFrameTimer = gameTime;
            mCurrentFrame +=1;
     
            if(mCurrentFrame >= mNumFrames) {
                mCurrentFrame = 0;
            }
            
            mSrcX = mSrcW*mCurrentFrame;
            this.setFrameClip(mSrcX, mSrcY, mSrcX+mSrcW, mSrcY+mSrcH);
            //this.setFrameClip((float)mCurrentFrame*6f/32f, 0f, (float)mCurrentFrame*6f/32f+ 6f/32f, 1f);
        }
    }
    public void setFrameNumber(int i) {
    	mCurrentFrame = i;
    	mSrcX = mSrcW*mCurrentFrame;
        this.setFrameClip(mSrcX, mSrcY, mSrcX+mSrcW, mSrcY+mSrcH);
    }
}